package com.dckeji.shiplock.webapp.controller.rest;

import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.framework.util.StringUtil;
import com.dckeji.shiplock.webapp.controller.BaseController;
import com.dckeji.shiplock.webapp.pojo.Crew;
import com.dckeji.shiplock.webapp.pojo.LogicRsult;
import com.dckeji.shiplock.webapp.pojo.Owner;
import com.dckeji.shiplock.webapp.pojo.UserInfo;
import com.dckeji.shiplock.webapp.service.IOwnerService;
import com.dckeji.shiplock.webapp.util.ReturnMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.Oneway;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/11
 * Time: 10:03
 * 船主
 */
@RequestMapping("/rest/owner/")
@RestController
public class OwnerController extends BaseController {

    @Autowired
    IOwnerService ownerService;

    @RequestMapping("userInfo")
    public LogicRsult queryUserInfoByWechat(@RequestBody UserInfo u) {
        LogicRsult logicRsult = new LogicRsult();
        if (valida(u)) {
            UserInfo userInfo = ownerService.queryUserInfoByWechat(u.getOpenid());
            if (null != userInfo) {
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
                logicRsult.setObj(userInfo);
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.NODATE);
            }
        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.VALIDA);
        }

        return logicRsult;
    }

    /**
     * 船主认证
     *
     * @param owner
     * @return
     */
    @RequestMapping(value = "auth")
    public LogicRsult ownerAuthentication(@RequestBody Owner owner) {
        LogicRsult logicRsult = new LogicRsult();
        try {
            UserInfo userInfo = ownerService.queryUserInfoByWechat(owner.getOpenid());
            if (null == userInfo) {
                int row = ownerService.ownerAuthentication(owner);
                if (row != SystemConstants.ResultCode.UNIQUE) {
                    if (row > 0) {
                        logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                        logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
                    } else {
                        logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                        logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
                    }
                } else {
                    logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                    logicRsult.setResultMsg(SystemConstants.ErrorMsg.UNIQUE);
                }
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.UNIQUE);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
        }

        return logicRsult;
    }

    /**
     * 船长添加船员
     *
     * @param crew
     * @return
     */
    @RequestMapping("addCrew")
    public LogicRsult addCrew(@RequestBody Crew crew) {
        LogicRsult logicRsult = new LogicRsult();

        try {
            int row = ownerService.addCrew(crew);
            if (row > 0) {
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);

            }
        } catch (Exception e) {
            e.printStackTrace();
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
        }

        return logicRsult;
    }

    /**
     * 个人中心船员信息（已绑定的船员）
     *
     * @param owner
     * @return
     */
    @RequestMapping("queryCrewInfos")
    public LogicRsult queryCrewInfos(@RequestBody Owner owner) {
        LogicRsult logicRsult = new LogicRsult();

        if (!StringUtil.isNull(owner)) {
            List<Crew> crews = ownerService.queryCrewInfos(owner.getOwnerId());
            if (!StringUtil.isNull(crews)) {
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);

                logicRsult.setObj(crews);
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
            }
        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.VALIDA);
        }
        return logicRsult;
    }

    /**
     * 船员绑定
     *
     * @param crew
     * @return
     */
    @RequestMapping("crewAuth")
    public LogicRsult crewAuth(@RequestBody Crew crew) {
        LogicRsult logicRsult = new LogicRsult();
        int code = ownerService.crewAuth(crew);
        if (code != SystemConstants.ResultCode.NODATE) {
            if (code > 0) {
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
            }
        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.NODATE);
        }
        return logicRsult;
    }

    /**
     * 船长删除船员
     *
     * @return
     */
    @RequestMapping("deleteCrew")
    public LogicRsult deleteCrew(@RequestBody Crew crew) {
        if (valida(crew.getOpenid())) {
            UserInfo userInfo = ownerService.queryUserInfoByWechat(crew.getOpenid());
            if (isOwner(userInfo)) {
                int row = ownerService.deleteCrew(crew.getCrewId());
                if (row > 0) {
                    return ReturnMsgUtil.success();
                } else {
                    return ReturnMsgUtil.busError("删除失败！");
                }
            } else {
                return ReturnMsgUtil.busError("您不是船长，无权限操作！");
            }
        } else {

            return ReturnMsgUtil.valiError();
        }

    }

    /**
     * 船长修改船员信息
     *
     * @param crew
     * @return
     */
    @RequestMapping("updateCrew")
    public LogicRsult updateCrew(@RequestBody Crew crew) {
        if (valida(crew.getOpenid())) {
            UserInfo userInfo = ownerService.queryUserInfoByWechat(crew.getOpenid());
            if (isOwner(userInfo)) {
                int row = ownerService.updateCrew(crew);
                if (row > 0) {
                    return ReturnMsgUtil.success();
                } else {
                    return ReturnMsgUtil.busError("修改失败！");
                }
            } else {
                return ReturnMsgUtil.busError("您不是船长，无权限操作！");
            }
        } else {

            return ReturnMsgUtil.valiError();
        }
    }

    @RequestMapping("updatePwd")
    public LogicRsult updatePwd(@RequestBody UserInfo userInfo) {
        UserInfo userInfo1 = ownerService.queryUserInfo(userInfo.getOpenid());
        if (userInfo1.getOldPwd().equals(userInfo.getOldPwd())) {
            int row = ownerService.updatePwd(userInfo);
            if (row > 0) {
                return ReturnMsgUtil.success();
            } else {
                return ReturnMsgUtil.sysError();
            }
        } else {
            return ReturnMsgUtil.busError("密码错误!");
        }


    }


}
